package org.xxkk.utils;

import org.apache.commons.cli.*;
import org.xxkk.utils.bo.ExcelParser;

import java.io.*;
import java.util.Date;

//jar cvfm excel2XJT.jar MANIFEST.MF ../../java/org/xxkk/utils/Main.java

/**
 * @author xxkk
 * 将本工具特定规则书写的excel文件转换成特定规则的文件 xml、json、ts、.d.ts、i18n（ts文件）
 */
public class Main {

    public static Date date_start;

    public static void main(String[] args) {//可选传入参数 dirPath=./excel  outPath=./  -outType='xml,json,ts,tsd'
        try {
            boolean isMac = false;
            date_start = new Date();
            String cur_path = new File("").getCanonicalPath();// 当前工作路径
            String dirPath = args.length > 0 ? args[0] : cur_path + (isMac ? "/excel" : "\\excel");//;// excel存放路径
            String outPath = args.length > 1 ? args[1] : cur_path;// 文件输出路径

            ExcelParser excelParser = getExcleParser(args, outPath);
            File file = new File(dirPath);
            excelParser.parse(file);// 输出功能函数
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    private static ExcelParser getExcleParser(String[] args, String outPath) {
        ExcelParser excelParser = new ExcelParser();
        excelParser.setAbsPath(outPath);// 设置输出文件路径
        final Options options = new Options();// 解析命令行
        options.addOption("t", "outType", true, "output file type [,xml,json,ts,tsd,i18n]");
        final CommandLineParser parser = new PosixParser();
        try {
            CommandLine cmd = parser.parse(options, args);
            if (cmd.hasOption("t")) {
                excelParser.parseType = cmd.getOptionValue("t");
                if (excelParser.parseType.contains("tsd")) // 并发写入同一个文件需要创建输入流
                    setDTSFileWriteStream(excelParser);
                if (excelParser.parseType.contains("xml"))
                    makdir(excelParser.getXmlOutputDir());
                if (excelParser.parseType.contains("json"))
                    makdir(excelParser.getJsonOutputDir());
                if (excelParser.parseType.contains("ts"))
                    makdir(excelParser.getTsOutputDir());
                if (excelParser.parseType.contains("i18n"))
                    makdir(excelParser.getI18nOutputDir());
            } else {
                setDTSFileWriteStream(excelParser);
                makdir(excelParser.getXmlOutputDir());
                makdir(excelParser.getJsonOutputDir());
                makdir(excelParser.getTsOutputDir());
                makdir(excelParser.getI18nOutputDir());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return excelParser;
    }

    /**
     * 创建并设置 .d.ts 文件输出流，因为多线程对同一个文件输入所以需要提前构建输入流
     *
     * @param excelParser
     */
    private static void setDTSFileWriteStream(ExcelParser excelParser) {
        try {
            String path = excelParser.getTsDOutputDir();
            makdir(path);
            String pathFile = path + "excelGlobal.d.ts";
            File file = new File(pathFile);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), ExcelParser.format);
            BufferedWriter bw = new BufferedWriter(outputStreamWriter);
            excelParser.setOutputStreamWriter(outputStreamWriter);
            excelParser.setBufferedWriter(bw);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 判断目录是否存在,不能存在则创建
     *
     * @param path
     */
    public static void makdir(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
    }

//    private static File newFile(String path) throws IOException {
//        File file = new File(path);
//        if (!file.exists()) {
//            file.createNewFile();
//        }
//        return file;
//    }
}